package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.io.IOException;

public class SettingPage extends Utils {
    public static String url="http://8.155.1.153:8081/system/setting";
    public SettingPage() {
        super(url);
    }

    public void checkPageRight() throws IOException {
        //查看页面关键元素
        //基本设置
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div/div/div[1]/ul/li[1]/span")).click();
        //头像
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div/div/div[2]/div[2]/div[2]/div[2]/img"));
        //更换头像
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div/div/div[2]/div[2]/div[2]/span/div/span/div/button"));
        //姓名
        driver.findElement(By.xpath("//*[@id=\"name\"]"));
        //提交
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div/div/div[2]/div[2]/div[1]/form/div[5]/div[2]/button"));

        //安全设置
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div/div/div[1]/ul/li[2]/span")).click();
        //账户密码
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div/div/div[2]/div[2]/div/div/ul/li/div/div/h4"));
        //修改
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div/div/div[2]/div[2]/div/div/ul/li/ul/li/a"));
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    //修改密码
    public  void ChangePassword() throws IOException {
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div/div/div[2]/div[2]/div/div/ul/li/ul/li/a")).click();
        driver.findElement(By.xpath("//*[@id=\"oldPassword\"]")).sendKeys("123456");
        driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("111111");
        driver.findElement(By.xpath("//*[@id=\"confirmPassword\"]")).sendKeys("111111");
        driver.findElement(By.xpath("/html/body/div[4]/div/div[2]/div/div[2]/div[3]/div/div/div[2]/button")).click();
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    //更换头像
    public void ChangeAvatar() throws IOException {
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div/div/div[1]/ul/li[1]/span")).click();
        WebElement fileInput = wait.until(ExpectedConditions.presenceOfElementLocated(
                By.cssSelector("input[type='file'][accept*='image']")
        ));

        //  要上传的图片文件的绝对路径
        String imagePath = "D:/AAFiles/Learn/Test/Questionnaire/One.png";

        //将文件路径发送给隐藏的input元素
        fileInput.sendKeys(imagePath);
        WebElement confirmButton = driver.findElement(By.cssSelector("div.ant-modal-footer button.ant-btn-primary"));
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].click();", confirmButton);

        //头像是否显示
        // 检查图片是否成功加载和显示
        WebElement image = driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div/div/div[2]/div[2]/div[2]/div[2]/img"));
        assert image.isDisplayed() : "图片未显示";
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());

    }
}
